import {
  checkLogin
} from './auth.js'
new Vue({
  el: '#app',
  data: {
    IS_LOGIN: null,
    reportData: {}
  },
  async created() {
    this.IS_LOGIN = await checkLogin()
    if (this.IS_LOGIN == null) {
      location.href = '/'
    }
    await this.getReportData()
  },
  methods: {
    async getReportData() {
      const res = await request.get('/report/getBusinessReport')
      this.reportData = res.data
      console.log(this.reportData)
    },
    // 导出Excel
    async exportExcel() {
      try {
        const res = await request({
          url: '/report/getBusinessReportExcel',
          method: 'get',
          responseType: 'blob'
        })
        // 从响应头中获取文件名（如果后端提供了的话）
        const contentDisposition = res.headers['content-disposition'];
        let filename = 'business-report.xlsx';
        if (contentDisposition) {
          const filenameMatch = contentDisposition.match(/filename="?(.+)"?/);
          if (filenameMatch && filenameMatch[1]) {
            filename = filenameMatch[1];
          }
        }
        const blob = new Blob([res.data])
        const downloadUrl = window.URL.createObjectURL(blob)

        // 创建一个隐藏的<a>元素用于触发下载
        const link = document.createElement('a')
        link.href = downloadUrl
        link.download = 'business-report.xlsx' // 设置下载文件名
        link.style.display = 'none'

        // 添加到页面并触发点击
        document.body.appendChild(link)
        link.click()
        // 清理：移除链接和释放URL对象
        document.body.removeChild(link)
        window.URL.revokeObjectURL(downloadUrl)
      } catch (error) {
        console.error('文件下载失败:', error)
        this.$message.error('文件下载失败')
      }
    }
  }
})